package com.qbw.app.module.common.view;

import android.content.Context;
import androidx.databinding.DataBindingUtil;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.qbw.app.R;
import com.qbw.app.databinding.CommonViewNumberKeyboardBinding;

/**
 * @author QBW
 * @date 2019/9/16
 */
public class NumberKeyboardView extends FrameLayout implements View.OnClickListener {

    private CommonViewNumberKeyboardBinding mBinding;

    private Listener mListener;

    public NumberKeyboardView(@NonNull Context context) {
        super(context);
        init(context);
    }

    public NumberKeyboardView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public NumberKeyboardView(@NonNull Context context, @Nullable AttributeSet attrs,
                              int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mBinding = DataBindingUtil.inflate(LayoutInflater.from(context),
                                           R.layout.common_view_number_keyboard, this, true);
        mBinding.tv0.setOnClickListener(this);
        mBinding.tv1.setOnClickListener(this);
        mBinding.tv2.setOnClickListener(this);
        mBinding.tv3.setOnClickListener(this);
        mBinding.tv4.setOnClickListener(this);
        mBinding.tv5.setOnClickListener(this);
        mBinding.tv6.setOnClickListener(this);
        mBinding.tv7.setOnClickListener(this);
        mBinding.tv8.setOnClickListener(this);
        mBinding.tv9.setOnClickListener(this);
        mBinding.tvX.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int vid = v.getId();
        int n = -2;
        if (vid == R.id.tv_0) {
            n = 0;
        } else if (vid == R.id.tv_1) {
            n = 1;
        } else if (vid == R.id.tv_2) {
            n = 2;
        } else if (vid == R.id.tv_3) {
            n = 3;
        } else if (vid == R.id.tv_4) {
            n = 4;
        } else if (vid == R.id.tv_5) {
            n = 5;
        } else if (vid == R.id.tv_6) {
            n = 6;
        } else if (vid == R.id.tv_7) {
            n = 7;
        } else if (vid == R.id.tv_8) {
            n = 8;
        } else if (vid == R.id.tv_9) {
            n = 9;
        } else if (vid == R.id.tv_x) {
            n = -1;
        }
        if (n >= 0) {
            if (mListener != null) {
                mListener.onNumberKeyboardClickNumber(n);
            }
        } else if (n == -1) {
            if (mListener != null) {
                mListener.onNumberKeyboardClickBack();
            }
        }
    }

    public interface Listener {
        void onNumberKeyboardClickNumber(int number);

        void onNumberKeyboardClickBack();
    }

    public void setListener(Listener listener) {
        mListener = listener;
    }
}
